package com.wallpaper.weatherwallpaper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WallPaperAdapter extends BaseAdapter{
	private Context mContext;
	private String[] weathers;
	private String[] times;
	private Integer[] systemwallpapers;
	private String[] userwallpapers;

	public WallPaperAdapter(Context c,String[] w,String[] t,Integer[] swp, String[] uwp){
		mContext=c;
		weathers=w;
		times=t;
		systemwallpapers=swp;
		userwallpapers=uwp;
		System.gc();//优化内存溢出
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return systemwallpapers.length;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LinearLayout ll;
		if(convertView==null)
		{  
			ll=new LinearLayout(mContext);
			ll.setLayoutParams(new GridView.LayoutParams(85, 85));
			ll.setPadding(8,8,8,8);  
		}  
		else  
		{
			ll=(LinearLayout) convertView;  
		}
		ll.setOrientation(LinearLayout.VERTICAL);
		
		TextView tv=new TextView(mContext);
		tv.setText(weathers[position]+" "+times[position]);
		tv.setTextSize(10);
		ll.addView(tv);
		
		ImageView imageview=new ImageView(mContext);
		imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
		Bitmap bm=null;
		if(systemwallpapers[position]!=0){//使用的是系统图库
			bm=BitmapFactory.decodeResource(mContext.getResources(),systemwallpapers[position]);				
		}else {
			if(userwallpapers[position]!=null){//使用的是用户图库
				bm=BitmapFactory.decodeFile(userwallpapers[position]);
			}
		}
		if(bm!=null){
			Bitmap scaledBm=Bitmap.createScaledBitmap(bm, 85, 70,true);//将原图片缩略成固定大小，优化内存溢出
			bm.recycle();//及时清理处理过的Bitmap，优化内存溢出

			imageview.setImageBitmap(scaledBm);
			ll.addView(imageview);
		}
		
		return ll;
	}

	public String[] getWeathers(){
		return weathers;
	}
	
	public String[] getTimes(){
		return times;
	}
	
	public Integer[] getSystemwallpapers(){
		return systemwallpapers;
	}
	
	public String[] getUserwallpapers(){
		return userwallpapers;
	}
}
